package com.neusoft.weekreport.component.controller;

import com.neusoft.weekreport.component.bizService.ProductBizService;
import com.neusoft.weekreport.component.req.GetAddProductInfoReq;
import com.neusoft.weekreport.component.req.GetProductInfoListReq;
import com.neusoft.weekreport.component.resp.GetAddProductInfoResp;
import com.neusoft.weekreport.component.resp.GetProductInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetProductInfoListResp;
import com.neusoft.weekreport.component.resp.ResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@EnableTransactionManagement
@RequestMapping("/product")

public class ProductController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    ProductBizService productBizService;

    //查看产品列表
    @RequestMapping(value = "/productManage",method = RequestMethod.GET)
    public ResponseBean<GetProductInfoListResp> getProductInfoList(GetProductInfoListReq req, HttpServletRequest request){
        ResponseBean<GetProductInfoListResp> responseBean = new ResponseBean<>();
        try{
            GetProductInfoListResp  resp = productBizService.getProductInfoList(req);
            responseBean = new ResponseBean<>(HttpStatus.OK.value(),"查询产品列表成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return responseBean;
    }

    //添加产品信息
    @RequestMapping(value = "/productManage",method = RequestMethod.POST)
    public ResponseBean<GetAddProductInfoResp> addProductInfo(@RequestBody GetAddProductInfoReq req, HttpServletRequest request){
        ResponseBean<GetAddProductInfoResp> responseBean = new ResponseBean<>();
        try{
            GetAddProductInfoResp resp = productBizService.addProductInfo(req);
            responseBean = new ResponseBean<>(HttpStatus.OK.value(),"添加产品信息成功",resp);

        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return responseBean;
    }
    //修改产品信息
    @RequestMapping(value = "/productManage",method = RequestMethod.PUT)
    public ResponseBean modifyProductInfo(@RequestBody GetAddProductInfoReq req,HttpServletRequest request){
        ResponseBean responseBean =new ResponseBean<>();
        try{
            productBizService.modifyProductInfo(req);
            responseBean = new ResponseBean<>(HttpStatus.OK.value(),"修改产品信息成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return responseBean;
    }

    //删除产品信息
    @RequestMapping(value ="/productManage",method = RequestMethod.DELETE)
    public ResponseBean deleteProductInfo(@RequestBody  GetAddProductInfoReq req,HttpServletRequest request){
        ResponseBean responseBean =new ResponseBean();
        try{
            productBizService.deleteProductInfo(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"删除产品信息成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return responseBean;
    }

    //查看产品信息
    @RequestMapping(value="/productInfoById",method = RequestMethod.GET)
    public ResponseBean<GetProductInfoByIdResp> getProductInfoById(GetAddProductInfoReq req,HttpServletRequest request){
        ResponseBean<GetProductInfoByIdResp> responseBean =new ResponseBean();
        try{
            GetProductInfoByIdResp resp =productBizService.getProductInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询产品信息成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return responseBean;
    }
    //改变产品信息
    @RequestMapping(value="/changeProductStatus",method = RequestMethod.PUT)
    public ResponseBean changeProductStatus(@RequestBody GetAddProductInfoReq req,HttpServletRequest request){
        ResponseBean responseBean =new ResponseBean();
        try{
            productBizService.changeProductStatus(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"改变产品状态成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return responseBean;
    }

    //查询有效产品
    @RequestMapping(value="/validProduct",method = RequestMethod.GET)
    public ResponseBean<GetProductInfoListResp> getValidProduct(GetProductInfoListReq req,HttpServletRequest request){
        ResponseBean<GetProductInfoListResp> responseBean = new ResponseBean();
        try{
            GetProductInfoListResp  resp = productBizService.getValidProductList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询有效产品列表成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return responseBean;
    }


}
